package com.utils;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.dict.comment.domain.entity.EventWaring;
import com.sq.common.web.R;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class JsonExample {

    public JsonExample() {
    }

    public static EventWaring getMessage(String jsonStr) {
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        String imgbase64 = jsonObject.getString("imgbase64");
        String deviceId = jsonObject.getString("deviceId");
        String eventTypeId = jsonObject.getString("eventTypeId");
        String time = jsonObject.getString("time");
        Integer personImageId = jsonObject.getInteger("personImageId");
        Integer dataSource = jsonObject.getInteger("dataSource");
        String channelId = String.valueOf(jsonObject.getInteger("channelId"));
        Double score = 0.0;
        String rect = "";
        JSONArray detRegions = jsonObject.getJSONArray("detRegions");
        if (!detRegions.isEmpty()) {
            JSONObject firstDetRegion = detRegions.getJSONObject(0);
            JSONArray objects = firstDetRegion.getJSONArray("objects");
            if (!objects.isEmpty()) {
                JSONObject firstObject = objects.getJSONObject(0);
                score = firstObject.getDouble("score");
                rect = firstObject.getString("rect");}}
        EventWaring eventWaring = new EventWaring();
        eventWaring.setChannelId(channelId);
        eventWaring.setImgbase64(imgbase64);
        eventWaring.setDeviceId(deviceId);
        eventWaring.setEventTypeId(eventTypeId);
        eventWaring.setDataSource(dataSource);
        eventWaring.setPersonImageId(personImageId);
        eventWaring.setTime(time);
        eventWaring.setScore(score);
        eventWaring.setRect(rect);
        return eventWaring;
    }



    public static String getNowDate() {
        // 获取当前日期和时间
        LocalDateTime now = LocalDateTime.now();

        // 定义格式化器，精确到毫秒
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");

        // 格式化当前日期和时间
        String formattedDateTime = now.format(formatter);
        return formattedDateTime;
    }
    public static String extractBase64Data(String base64Image) {
        // 找到逗号的位置
        int commaIndex = base64Image.indexOf(',');

        // 如果找到了逗号，则提取逗号之后的部分
        if (commaIndex != -1) {
            return base64Image.substring(commaIndex + 1);
        }

        // 如果没有找到逗号，则返回原始字符串
        return base64Image;
    }
}
